﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.NodeFactory;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class ContentMedicineCategoryDetails : System.Web.UI.MasterPage
    {
        public string CategoryName { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CategoryName = Request["catName"] != null ? Request["catName"].ToString() : "";
                int tempInt;
                int id = int.TryParse(CategoryName, out tempInt) ? tempInt : 0;
                
                Node categoryNode = new Node(id);

                if (categoryNode != null
                    && !string.IsNullOrEmpty(categoryNode.Name)
                    && categoryNode.NodeTypeAlias == "MedicineCategory")
                {
                    var node = ContentHelper.GetByNodeId<MedicineCategory>(id);
                    CategoryName = categoryNode.Name;
                    litDescription.Text = node.Description;
                    var GenericList = ContentHelper.GetByNodeId<MedicineCategory>(categoryNode.Id).GetChildren<MedicineGenericName>();
                    rptMedList.DataSource = GenericList;
                    rptMedList.DataBind();
                    ShowContent(true);
                }
                else
                {
                    ShowContent(false);
                }
            }
            
        }
        void ShowContent(bool b)
        {
            plcContent.Visible = b;
            plcError.Visible = !b;
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/medicines");
        }
        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                MedicineGenericName gen = (MedicineGenericName)e.Item.DataItem;
                if (!string.IsNullOrEmpty(gen.Name))
                {
                    Literal litMedicineCount = (Literal)e.Item.FindControl("litMedicineCount");

                    litMedicineCount.Text = gen.Children().Count().ToString();
                }
            }
        }

    }
}